package test;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import com.rfid.uhf288.Device;
import com.sast.sql.DataBase;

public class RFID extends Thread{
	
	static final String ip =  "192.168.1.100";
	static final int port = 27011;
	
	public Boolean runningFlag = true;
 	private Device reader = null;
	private byte[] comAddr = new byte[1];
	private int[] portHandle = new int[1];
	
	public RFID() {
		//初始化连接
		System.loadLibrary("com_rfid_uhf288_Device");
		reader = new Device();
		comAddr[0]=(byte)255;
		int result = reader.OpenNetPort(port,ip, comAddr, portHandle);
		System.out.println(result);
		if (result != 0) 
			return;
		System.out.println("连接成功");
	}
	
	public void working() {
		//EPCC1-G2操作
		byte QValue=4,Session=0,MaskMem=2;
		byte[]MaskAdr=new byte[2];
		byte MaskLen=0;
		byte[]MaskData=new byte[256];
		byte MaskFlag=0,AdrTID=0,LenTID=6,TIDFlag=1,Target=0,InAnt=(byte)0x80;
		byte Scantime=10,FastFlag=0;
		byte[]pEPCList=new byte[20000];
		int[]Totallen=new int[1];
		int[]CardNum=new int[1];
		byte[]ant = new byte[1];
		int result = reader.Inventory_G2(comAddr,QValue,Session,MaskMem,MaskAdr,MaskLen,MaskData,MaskFlag,
				AdrTID,LenTID,TIDFlag,Target,InAnt,Scantime,FastFlag,pEPCList, ant,Totallen,
				CardNum,portHandle[0]);  
		System.out.println("查询标签："+result);
		
		System.out.println("标签张数："+CardNum[0]);
		int m=0;
		for(int index=0;index<CardNum[0];index++){
			int epclen = pEPCList[m++]&255;
			String EPCstr="";
			byte[]epc = new byte[epclen];
			for(int n=0;n<epclen;n++){
				byte bbt = pEPCList[m++];
				epc[n] = bbt;
				String hex= Integer.toHexString(bbt& 255);
				if(hex.length()==1) 
					hex="0"+hex;	
				EPCstr+=hex;
			}
			int rssi = pEPCList[m++];
			System.out.println(EPCstr.toUpperCase());

			byte ENum=(byte)255,Mem=1,WordPtr=2,Num=6;
			byte[]Password=new byte[4];
			MaskMem=2;
			MaskAdr[0]=0;
			MaskAdr[1]=0;
			MaskLen=96;
			int p=0;
			System.arraycopy(epc,0,MaskData,0,MaskLen/8);
			byte[]Data=new byte[Num*2];
			int[]Errorcode=new int[1];
			WordPtr=2;
			result = reader.ReadData_G2(comAddr,epc,ENum,Mem,WordPtr,Num,Password,
					MaskMem,MaskAdr, MaskLen,MaskData,Data,Errorcode,portHandle[0]); 
			System.out.println("读数据"+result);
			if(result==0){
				String Memdata="";
				for( p=0;p<Num*2;p++){
					byte bbt = Data[p];
					String hex= Integer.toHexString(bbt& 255);
					if(hex.length()==1)
						hex="0"+hex;
					Memdata+=hex;
				}
				System.out.println("epc:"+Memdata+"\n");
				DataBase dBase = new DataBase();		
				SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
				String dateStr =format.format(new Date());
				String sql = "insert into Inventory_Log(epc_id,datetime)values(?,?)";
				dBase.update(sql, Memdata, dateStr);

			}
		}
	}
	@Override
 	public void run() {
		// 间隔2s循环发送巡查指令
		while (runningFlag) {
			working();
			try {
				sleep(3000);
			} catch (InterruptedException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
		
	}
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new RFID().start();
	}
}
	



